import 'package:flutter/material.dart';
import 'package:transparent_image/transparent_image.dart';
import 'package:silver_flutter/model/chapter.dart';
import 'package:silver_flutter/api.dart' as api;
import 'package:silver_flutter/model/page.dart';

class ChapterPage extends StatefulWidget {
  final int bookId;
  final Chapter chapter;

  ChapterPage({Key key, this.bookId, this.chapter}) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return new _ChapterPageState();
  }
}

class _ChapterPageState extends State<ChapterPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new FutureBuilder<List<Page>>(
        future: api.fetchPages(widget.bookId, widget.chapter.number),
        builder: (context, snapshot) {
          return new CustomScrollView(
            slivers: <Widget>[
              new SliverAppBar(
                title: new Text('第${widget.chapter.number}话'),
                floating: true,
              ),
              new SliverList(
                delegate: new SliverChildBuilderDelegate(
                  (context, index) {
                    return new FadeInImage.memoryNetwork(
                        placeholder: kTransparentImage,
                        image: snapshot.data[index].imageUrl);
                  },
                  childCount: snapshot.hasData ? snapshot.data.length : 0,
                ),
              )
            ],
          );
        },
      ),
    );
  }
}
